package com.gy.assistant.util;

import android.content.Context;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.gy.assistant.R;

public class TimeTools {

    public static String longFormatTime(long time, Context context) {
        // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        date.setTime(time);
        if (isThisYear(date)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
            if (isToday(date)) {
                int minute = minutesAgo(time);
                if (minute < 60) {
                    if (minute <= 1) {
                        return context.getString(R.string.tx_time_just_now);
                    } else {
                        return minute + context.getString(R.string.tx_time_minutes_ago);
                    }
                } else {
                    return simpleDateFormat.format(date);
                }
            } else {
                if (isYestYesterday(date)) {//昨天，显示昨天
                    return context.getString(R.string.tx_time_yesterday) + simpleDateFormat.format(date);
                } else if (isThisWeek(date)) {//本周,显示周几
                    String weekday = null;
                    if (date.getDay() == 1) {
                        weekday = context.getString(R.string.tx_time_monday);
                    }
                    if (date.getDay() == 2) {
                        weekday = context.getString(R.string.tx_time_tuesday);
                    }
                    if (date.getDay() == 3) {
                        weekday = context.getString(R.string.tx_time_wednesday);
                    }
                    if (date.getDay() == 4) {
                        weekday = context.getString(R.string.tx_time_thursday);
                    }
                    if (date.getDay() == 5) {
                        weekday = context.getString(R.string.tx_time_friday);
                    }
                    if (date.getDay() == 6) {
                        weekday = context.getString(R.string.tx_time_saturday);
                    }
                    if (date.getDay() == 0) {
                        weekday = context.getString(R.string.tx_time_sunday);
                    }
                    return weekday + " " + simpleDateFormat.format(date);
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
                    return sdf.format(date);
                }
            }
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return sdf.format(date);
        }
    }

    private static int minutesAgo(long time) {
        return (int) ((System.currentTimeMillis() - time) / (60000));
    }

    private static boolean isToday(Date date) {
        Date now = new Date();
        return (date.getYear() == now.getYear()) && (date.getMonth() == now.getMonth()) && (date.getDate() == now.getDate());
    }

    private static boolean isYestYesterday(Date date) {
        Date now = new Date();
        return (date.getYear() == now.getYear()) && (date.getMonth() == now.getMonth()) && (date.getDate() + 1 == now.getDate());
    }

    private static boolean isThisWeek(Date date) {
        Date now = new Date();
        if ((date.getYear() == now.getYear()) && (date.getMonth() == now.getMonth())) {
            if (now.getDay() - date.getDay() < now.getDay() && now.getDate() - date.getDate() > 0 && now.getDate() - date.getDate() < 7) {
                return true;
            }
        }
        return false;
    }

    private static boolean isThisYear(Date date) {
        Date now = new Date();
        return date.getYear() == now.getYear();
    }

    static final private SimpleDateFormat sFormat = new SimpleDateFormat("yyMMddHHmmssSSS");
    static final private SimpleDateFormat sLogFormat = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");

    static public String getNowTimeStr() {
        return sFormat.format(new Date());
    }

    static public String getLogStr() {
        return sLogFormat.format(new Date());
    }

}
